package com.ophiux.utils.common;
 
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 说明：map工具类<br>
 * <h1>创 建 人: hehailong</h1>
 * 创建日期: 2019年10月8日 上午10:06:27<br>
 * 修改记录：
 * 需要的jar:json-lib-2.4-jdk15  commons-beanutils.jar，commons-httpclient.jar，commons-lang.jar，ezmorph.jar，morph-1.0.1.jar 
 */
public class MapTools {
    
	/**
	 * 说明：将json转List<Map<String,Object>><br>
	 * 创 建 人: <b>hehailong</b><br>
	 * @param jsonStr
	 * @return	List<Map<String,Object>>
	 */
    public static List<Map<String, Object>> parseJSON2List(String jsonStr){
        JSONArray jsonArr = JSONArray.fromObject(jsonStr);
        List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
        Iterator<JSONObject> it = jsonArr.iterator();
        while(it.hasNext()){
            JSONObject json2 = it.next();
            list.add(parseJSON2Map(json2.toString()));
        }
        return list;
    }
     
    /**
     * 说明：将json转Map<String,Object><br>
     * 创 建 人: <b>hehailong</b><br>
     * @param jsonStr
     * @return	Map<String,Object><
     */
    public static Map<String, Object> parseJSON2Map(String jsonStr){
        Map<String, Object> map = new HashMap<String, Object>();
        //最外层解析
        JSONObject json = JSONObject.fromObject(jsonStr);
        for(Object k : json.keySet()){
            Object v = json.get(k);
            //如果内层还是数组的话，继续解析
            if(v instanceof JSONArray){
                List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
                Iterator<JSONObject> it = ((JSONArray)v).iterator();
                while(it.hasNext()){
                    JSONObject json2 = it.next();
                    list.add(parseJSON2Map(json2.toString()));
                }
                map.put(k.toString(), list);
            }else if(v instanceof JSONObject){//内层是对象，继续解析
            	 Map<String, Object> submap = parseJSON2Map(v.toString());
            	 map.put(k.toString(), submap);
            } else {
                map.put(k.toString(), v);
            }
        }
        return map;
    }
     
    
    public static List<Map<String, Object>> getListByUrl(String url){
        try {
            //通过HTTP获取JSON数据
            InputStream in = new URL(url).openStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder sb = new StringBuilder();
            String line;
            while((line=reader.readLine())!=null){
                sb.append(line);
            }
            return parseJSON2List(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
     
    
    public static Map<String, Object> getMapByUrl(String url){
        try {
            //通过HTTP获取JSON数据
            InputStream in = new URL(url).openStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder sb = new StringBuilder();
            String line;
            while((line=reader.readLine())!=null){
                sb.append(line);
            }
            return parseJSON2Map(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
     
     
    //test
    public static void main(String[] args) {
        String url = "http://app.cdxt.com.cn:8088/rmc/mobile/web";
//        List<Map<String,Object>> list = getListByUrl(url);
//        System.out.println(list);
       Map<String, Object> dd = parseJSON2Map("{\"bs_code\":\"T_00002\",\"reqData\":{\"ins_code\":\"000000\",\"password\":\"ef2492888e824c28b5cf6ad0dae9d97c\",\"channel\":\"000000\",\"sys_type\":\"${sys_type}\"}}");
        System.out.println(111);
    }
}